package com.yisu.expressway.onedollar.activity;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONObject;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.reflect.TypeToken;
import com.will.network.images.round.CircleImageView;
import com.yisu.expressway.R;
import com.yisu.expressway.network.ApiClientHelper;
import com.yisu.expressway.network.ApiResult;
import com.yisu.expressway.network.ApiSetting;
import com.yisu.expressway.onedollar.adapter.PicListAdapter;
import com.yisu.expressway.onedollar.model.OrderShowItem;
import com.yisu.expressway.onedollar.widget.TitleView;
import com.yisu.expressway.utils.StringUtil;
import com.yisu.expressway.utils.ToastUtil;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Kaven on 2016/12/15.
 */

public class OrderShowDetailActivity extends BasicActivity {
    private static final String EXTRA_KEY_GOODS_RECORD_ID = "extra_key_goods_record_id";

    @Bind(R.id.title_bar)
    protected TitleView mTitleView;
    @Bind(R.id.iv_cover)
    protected CircleImageView mCoverIv;
    @Bind(R.id.listView)
    protected ListView mPicLv;
    @Bind(R.id.tv_winner)
    protected TextView mNameTv;
    @Bind(R.id.tv_time)
    protected TextView mTimeTv;
    @Bind(R.id.tv_evaluatecontent)
    protected TextView mContentTv;
    @OnClick({R.id.tv_see_more,R.id.tv_go})
    public void onClick(View view){
	    switch (view.getId()){
		    case R.id.tv_see_more:
			    ToastUtil.showMessage(OrderShowDetailActivity.this,getString(R.string.action_is_developing));
			    break;
		    case R.id.tv_go:
			    ToastUtil.showMessage(OrderShowDetailActivity.this,getString(R.string.action_is_developing));
			    break;
		    default:
			    break;
	    }
    }

    private long mRecordId;
    private OrderShowItem mData;

    public static void launch(Context context, long recordId) {
        Intent intent = new Intent(context, OrderShowDetailActivity.class);
        intent.putExtra(EXTRA_KEY_GOODS_RECORD_ID, recordId);
        context.startActivity(intent);
    }

    @Override
    protected int getThemeId() {
        return R.style.OneDollarTheme;
    }

    @Override
    protected int getInflateLayoutId() {
        return R.layout.activity_onedollar_order_show_detail;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void releasePresenter() {

    }

    @Override
    protected void getIntentData(Intent intent) {
        long longExtra = intent.getLongExtra(EXTRA_KEY_GOODS_RECORD_ID, -1);
        if (longExtra < 0) {
            finish();
            return;
        }
        mRecordId = longExtra;
    }

    @Override
    protected void getSavedBundleData(Bundle bundle) {

    }

    @Override
    protected void initViews() {
        fetchData();
    }

    private void fetchData() {
        if (mRecordId <= 0) {
            return;
        }
        Map<String, Object> params = new HashMap();

        params.put("recordid", mRecordId);
        ApiClientHelper.postApi(ApiSetting.getOrderShowInfo(), new TypeToken<OrderShowItem>() {
        }, new JSONObject(params), new Response.Listener<ApiResult<OrderShowItem>>() {
            @Override
            public void onResponse(ApiResult<OrderShowItem> result) {
                if (!result.success) {
                    showToast(result.getMsg());
                    return;
                }
                mData = result.getResult();
                refreshViews();

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                showToast(volleyError.getMessage());
            }
        }, this);
    }

    private void refreshViews() {
        if (mData == null) {
            return;
        }
        String evaluatepic = mData.evaluatePic;
        if (!TextUtils.isEmpty(evaluatepic)) {
            String[] split = evaluatepic.split(",");
            if (split.length >= 1) {
	            PicListAdapter picListAdapter = new PicListAdapter(this,
                        Arrays.asList(split), null);
	            mPicLv.setAdapter(picListAdapter);
            }
        }
        String userheadimg = mData.userHeadImg;
        if (!TextUtils.isEmpty(userheadimg)) {
            Glide.with(getApplicationContext()).load(userheadimg).asBitmap()
                    .placeholder(R.drawable.avatar_default).diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop().animate(R.anim.fade_in_image).into(mCoverIv);
        }else {
            mCoverIv.setImageDrawable(getResources().getDrawable(R.drawable.avatar_default));
        }
        mNameTv.setText(mData.userName);
        mTimeTv.setText(mData.createTime);
        mContentTv.setText(mData.evaluateContent);
    }

    @Override
    protected void bindEvents() {
        mTitleView.setOnTitleBarClickEvent(new TitleView.OnTitleBarClickEvent() {
            @Override
            public void onLeftClick(View view) {
                finish();
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });
    }

    @Override
    protected boolean isNeedLogin() {
        return false;
    }

    @Override
    protected void onLoginStateChange(boolean login) {

    }

    private void showToast(String toastMsg) {
        if (StringUtil.isNullOrEmpty(toastMsg)) {
            return;
        }
        ToastUtil.displayLongToastMsg(this, toastMsg);
    }

}
